package com.gitee.mars.tourserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mars.tourserver.entity.TourRoute;
import com.gitee.mars.tourserver.manager.TourClawerManager;
import com.gitee.mars.tourserver.request.TourHotelRequest;
import com.gitee.mars.tourserver.response.base.PageInfo;
import com.gitee.mars.tourserver.utils.CityUtils;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gitee.mars.tourserver.service.ITourHotelService;
import com.gitee.mars.tourserver.mapper.TourHotelMapper;
import com.gitee.mars.tourserver.entity.TourHotel;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;


/**
 * TourHotel 实现层
 *
 * @Author wq
 * @Date 2021-04-14
 **/
@Service
public class TourHotelServiceImpl extends ServiceImpl<TourHotelMapper, TourHotel> implements ITourHotelService {

    @Resource
    private TourClawerManager tourClawerManager;

    @Override
    public void acquire() throws BadHanyuPinyinOutputFormatCombination {
        List<String> citysPinYin = CityUtils.getCitysPinYin();
        for (String city : citysPinYin) {
            List<TourHotel> tourHotel = tourClawerManager.getTourHotel(city);
            tourHotel.forEach(this::save);
        }

    }

    @Override
    public PageInfo<TourHotel> list(TourHotelRequest request) {
        IPage<TourHotel> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<TourHotel> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(request.getName())) {
            wrapper.like(TourHotel::getName, request.getName());
        }
        if (StringUtils.isNotEmpty(request.getLocationInfo())) {
            wrapper.like(TourHotel::getLocationInfo, request.getLocationInfo());
        }
        wrapper.orderByDesc(TourHotel::getCreateTime);
        List<TourHotel> list = baseMapper.selectPage(page, wrapper).getRecords();
        if (!CollectionUtils.isEmpty(list)) {
            return new PageInfo<>(page.getTotal(), list);
        }
        return null;
    }

    @Override
    public TourHotel detail(Integer id) {
        return baseMapper.selectById(id);
    }
}